package com.wlyuan.open.scanner.dao.write;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wlyuan.open.scanner.dao.entity.FinanceBillDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author yuanjie
 * @since 2022-12-26
 */
public interface FinanceBillWriteMapper extends BaseMapper<FinanceBillDO> {

    @Update("TRUNCATE TABLE t_finance_bill")
    void truncate();

    @Update("insert into t_finance_bill(up,down,tenant_id,create_time) select index_id as up,Index_SrcID as down,Index_OwnerCompanyID,Index_InsertTime as createTime " +
            "from Balance_BillIndex where Index_Status in (2,4,8) and Index_BillType = 2 and (Index_SupplierSymbolid = 0 or Index_SupplierSymbolid is null) and (Index_DriverID = 0 or Index_DriverID is null) " +
            "and Index_SrcID > 0 " +
            "union all " +
            "select a.Index_ID as up,b.Index_ID as down,a.Index_OwnerCompanyID,Index_InsertTime as createTime from (select index_id,Index_OwnerCompanyID " +
            "from Balance_BillIndex where Index_Status in (2,4,8) and Index_BillType = 2 and (Index_SupplierSymbolid = 0 or Index_SupplierSymbolid is null) and (Index_DriverID = 0 or Index_DriverID is null) " +
            "and (Index_SrcID = 0 or Index_SrcID is null)) a " +
            "inner join Balance_BillIndex b on b.Index_SrcID = a.index_id and b.Index_Status in (2,4,8) " +
            "union all " +
            "select 0,index_id,Index_OwnerCompanyID,Index_InsertTime as createTime " +
            "from Balance_BillIndex where Index_Status in (2,4,8) and Index_CustomerID in (select Customer_ID from TMS_MCustomer where Customer_UnderLine = 0) " +
            "union all " +
            "select Index_Id,0,Index_OwnerCompanyID,Index_InsertTime as createTime from Balance_BillIndex where Index_Status in (2,4,8) and (Index_SupplierSymbolid > 0 or Index_DriverID > 0)")
    void init();

    @Update("insert into t_finance_bill(up,down,tenant_id,create_time) select c.up as up,c.down as down,c.Index_OwnerCompanyID as tenantId,c.createTime as createTime from (select index_id as up,Index_SrcID as down,Index_OwnerCompanyID,Index_InsertTime as createTime " +
            "from Balance_BillIndex where Index_Status in (2,4,8) and Index_BillType = 2 and (Index_SupplierSymbolid = 0 or Index_SupplierSymbolid is null) and (Index_DriverID = 0 or Index_DriverID is null) " +
            "and Index_SrcID > 0 and Index_InsertTime >= #{startTime} and Index_InsertTime <= #{endTime} " +
            "union all " +
            "select a.Index_ID as up,b.Index_ID as down,a.Index_OwnerCompanyID,Index_InsertTime as createTime from (select index_id,Index_OwnerCompanyID " +
            "from Balance_BillIndex where Index_Status in (2,4,8) and Index_BillType = 2 and (Index_SupplierSymbolid = 0 or Index_SupplierSymbolid is null) and (Index_DriverID = 0 or Index_DriverID is null) " +
            "and (Index_SrcID = 0 or Index_SrcID is null) and Index_InsertTime >= #{startTime} and Index_InsertTime <= #{endTime}) a " +
            "inner join Balance_BillIndex b on b.Index_SrcID = a.index_id and b.Index_Status in (2,4,8) " +
            "union all " +
            "select 0,index_id,Index_OwnerCompanyID,Index_InsertTime as createTime " +
            "from Balance_BillIndex where Index_Status in (2,4,8) and Index_CustomerID in (select Customer_ID from TMS_MCustomer where Customer_UnderLine = 0) and Index_InsertTime >= #{startTime} and Index_InsertTime <= #{endTime} " +
            "union all " +
            "select Index_Id,0,Index_OwnerCompanyID,Index_InsertTime as createTime from Balance_BillIndex where Index_Status in (2,4,8) and (Index_SupplierSymbolid > 0 or Index_DriverID > 0) and Index_InsertTime >= #{startTime} and Index_InsertTime <= #{endTime}) c order by c.createTime asc")
    void initTime(@Param("startTime") String startTime, @Param("endTime") String endTime);
}
